package server

import (
	"errors"
	"math/rand"
	"shortvideo2zk/common/models/mysql"
	"shortvideo2zk/common/models/redis"
	user "shortvideo2zk/common/proto"
)

func SendSms(in *user.SendSmsReq) (*user.SendSmsResp, error) {
	code := rand.Intn(9000) + 1000
	/*sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	if err != nil {
		return nil, errors.New("短信发送错误")
	}
	if *sms.Body.Code != "OK" {
		return nil, errors.New(*sms.Body.Message)
	}*/
	redis.SetRedis("SendSms"+in.Source+in.Mobile, code)
	return &user.SendSmsResp{Greet: "发送成功"}, nil
}
func UserRegister(in *user.UserRegisterReq) (*user.UserRegisterResp, error) {
	getRedis := redis.GetRedis("SendSmsRegister" + in.Mobile)
	if getRedis != int(in.Code) {
		return nil, errors.New("验证码错误")
	}
	u := &mysql.User{}
	mobile, err := u.FindUserByMobile(in.Mobile)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	if mobile.ID != 0 {
		return nil, errors.New("该手机号已注册")
	}
	err = u.CreateUser(in.Mobile)
	if err != nil {
		return nil, errors.New("注册失败")
	}
	return &user.UserRegisterResp{Greet: "注册成功"}, nil
}
func UserLogin(in *user.UserLoginReq) (*user.UserLoginResp, error) {
	getRedis := redis.GetRedis("SendSmsLogin" + in.Mobile)
	if getRedis != int(in.Code) {
		return nil, errors.New("验证码错误")
	}
	u := &mysql.User{}
	mobile, err := u.FindUserByMobile(in.Mobile)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	if mobile.ID == 0 {
		return nil, errors.New("该手机号还未注册")
	}
	return &user.UserLoginResp{Greet: int64(mobile.ID)}, nil
}
func VideoList(in *user.VideoListReq) (*user.VideoListResp, error) {
	v := &mysql.Video{}

	page := in.Page
	if page < 1 {
		page = 1
	}

	pageSize := in.PageSize

	if pageSize < 10 {
		pageSize = 10
	}
	offset := (page - 1) * pageSize

	id, err := v.FindVideoByUserId(in.UserId, offset, pageSize)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	var Item []*user.VideoList
	for _, m := range id {
		Item = append(Item, &user.VideoList{
			Title: m.Title,
			Desc:  m.Desc,
			Url:   m.Url,
		})
	}
	return &user.VideoListResp{List: Item}, nil
}
func AddVideo(in *user.AddVideoReq) (*user.AddVideoResp, error) {
	if in.Title == "" {
		return nil, errors.New("标题不能为空")
	}
	if in.Desc == "" {
		return nil, errors.New("描述不能为空")
	}

	v := &mysql.Video{}

	err := v.CreateVideo(in)
	if err != nil {
		return nil, errors.New("发布失败")
	}
	return &user.AddVideoResp{Greet: "发布成功"}, nil

}
func FindVideo(in *user.FindVideoReq) (*user.VideoListResp, error) {
	v := &mysql.Video{}
	word, err := v.FinVideoByKeyWord(in.KeyWord)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	var Item []*user.VideoList
	for _, m := range word {
		Item = append(Item, &user.VideoList{
			Title: m.Title,
			Desc:  m.Desc,
			Url:   m.Url,
		})
	}
	return &user.VideoListResp{List: Item}, nil
}
func UpdateVideoStatus(in *user.UpdateVideoStatusReq) (*user.UpdateVideoStatusResp, error) {
	v := &mysql.Video{}

	err := v.UpdateVideoStatus(in.VideoId, in.Status)
	if err != nil {
		return nil, errors.New("修改失败")
	}
	return &user.UpdateVideoStatusResp{Greet: "修改成功"}, nil
}
func VideoSort(in *user.VideoSortReq) (*user.VideoListResp, error) {
	v := &mysql.Video{}
	order, err := v.VideoOrder()
	if err != nil {
		return nil, errors.New("查询失败")
	}
	var Item []*user.VideoList
	for _, m := range order {
		Item = append(Item, &user.VideoList{
			Title: m.Title,
			Desc:  m.Desc,
			Url:   m.Url,
		})
	}
	return &user.VideoListResp{List: Item}, nil
}
func DelVideo(in *user.DelVideoReq) (*user.DelVideoResp, error) {
	v := &mysql.Video{}
	u := &mysql.User{}
	id, err := u.FindUserById(in.UserId)
	if err != nil {
		return nil, err
	}
	if id.Level != 2 {
		return nil, errors.New("用户权限不足")
	}
	err = v.DelVideo(in.VideoId)
	if err != nil {
		return nil, errors.New("删除失败")
	}
	return &user.DelVideoResp{Greet: "删除成功"}, nil
}
